package com.hlk.utils;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 校验工具
 *
 * @author hlk
 * @version 1.0
 * @date 2020-12-18 15:18
 */
public class ValidateUtils {

    /**
     * 校验BO参数是否错误
     *
     * @param result
     * @return
     */
    public static Map<String, String> checkParams(BindingResult result) {
        HashMap<String, String> map = new HashMap<>();
        List<FieldError> list = result.getFieldErrors();
        list.forEach(error -> {
            // 发生错误的某个属性名
            String field = error.getField();
            // 验证的错误信息
            String msg = error.getDefaultMessage();
            map.put(field, msg);
        });
        return map;
    }
}
